package com.kx.yooli.kx.wechat.utils.log;

import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author john_sqwen-childe.wen
 * date 2024/4/8
 * PROJECT_NAME wechat
 * description create LogMonitorUtil by john_sqwen
 * and  value is
 */
public class LogMonitorUtil {
    private final Logger log = LoggerFactory.getLogger(LogMonitorUtil.class);
    private static final String URL = "http://lixin360.com/kxwechat";

    /**
     * 构建业务日志字符串
     * @param logId				日志编号
     * @param clazz				调用该方法的类对象
     * @param methodName		调用的方法名
     * @param interfaceName		调用的目标接口名
     * @param msg				附加信息
     * @param e
     */
    public static void generateBusinessLog(String logId, Class<?> clazz, String methodName, String interfaceName){
        try{
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("logID", logId);
            map.put("methodName", methodName);
            map.put("interfaceName", interfaceName);
            Logger logger = LoggerFactory.getLogger(clazz == null?"root":clazz.getSimpleName());
            BusinessLog log = BusinessLog.create(null, null, null, null, URL, map);
            logger.info(log.toString());
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    /**
     * 构建异常日志字符串
     * @param logId				日志编号
     * @param clazz				调用该方法的类对象
     * @param methodName		调用的方法名
     * @param interfaceName		调用的目标接口名
     * @param e					异常信息
     */
    public static void generateExceptionLog(String logId, Class<?> clazz, String methodName, String interfaceName, Exception e){
        try{
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("logID", logId);
            map.put("methodName", methodName);
            map.put("interfaceName", interfaceName);
            JSONObject json = JSONObject.from(map);
            Logger logger = LoggerFactory.getLogger(clazz == null?"root":clazz.getSimpleName());
            ExceptionLog ex = ExceptionLog.create(e, json.toString());
            logger.error(ex.toString());
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
